from django.db import models
from django.contrib.auth.models import PermissionsMixin
from django.contrib.auth.base_user import AbstractBaseUser
from django.contrib.auth.base_user import BaseUserManager
from django.utils.translation import ugettext_lazy as _
# from django.contrib.auth.models import User
# from django.db.models.signals import post_save
# from django.dispatch import receiver
# Create your models here.


class UserManager(BaseUserManager):
    use_in_migrations = True

    def _create_user(self, username, password, **extra_fields):
        """
        Creates and saves a User with the given email and password.
        """
        if not username:
            raise ValueError('The given username must be set')
        user = self.model(username=username, **extra_fields)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_user(self, username, password=None, **extra_fields):
        extra_fields.setdefault('is_superuser', False)
        return self._create_user(username, password, **extra_fields)

    def create_superuser(self, username, password, **extra_fields):
        extra_fields.setdefault('is_superuser', True)

        if extra_fields.get('is_superuser') is not True:
            raise ValueError('Superuser must have is_superuser=True.')

        return self._create_user(username, password, **extra_fields)


class Role(models.Model):
    code = models.CharField(max_length=25, verbose_name='编码')
    name = models.CharField(max_length=25, verbose_name='名称')

    created = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    comment = models.CharField(max_length=255, verbose_name='备注', null=True, blank=True)

    def __str__(self):
        return self.name
class Menu(models.Model):
    path = models.CharField(max_length=255, verbose_name='路径')
    code = models.CharField(max_length=25, verbose_name='编码')
    name = models.CharField(max_length=25, verbose_name='名称')
    icon = models.CharField(max_length=25, verbose_name='图标', null=True)
    parent = models.ForeignKey('self', verbose_name='父菜单', default=None, null=True, on_delete=models.CASCADE, related_name='children')

    created = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    comment = models.CharField(max_length=255, verbose_name='备注', null=True, blank=True)

    def __str__(self):
        return self.path


    class Meta:
        unique_together=('parent', 'path')
class User(AbstractBaseUser, PermissionsMixin):
    username = models.CharField(max_length=25, verbose_name='用户名', unique=True)
    name = models.CharField(max_length=25, verbose_name='姓名', blank=True, null=True)
    mobile = models.CharField(max_length=11, verbose_name='手机号', blank=True, null=True)
    email = models.CharField(max_length=50, verbose_name='邮箱', blank=True, null=True)
    is_active = models.BooleanField(_('active'), default=True)
    is_staff = models.BooleanField(_('staff'), default=True)
    created = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    comment = models.CharField(max_length=255, verbose_name='备注', null=True, blank=True)

    objects = UserManager()

    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = []

    class Meta:
        verbose_name = _('user')
        verbose_name_plural = _('users')

    def get_full_name(self):
        full_name = self.name
        return full_name.strip()

    def get_short_name(self):
        return self.name

    def email_user(self, subject, message, from_email=None, **kwargs):
        '''
        Sends an email to this User.
        '''
        send_mail(subject, message, from_email, [self.email], **kwargs)
#
# class Profile(models.Model):
#     user = models.OneToOneField(User, on_delete=models.CASCADE)
#     name = models.CharField(max_length=25, verbose_name='姓名')
#     mobile = models.CharField(max_length=11, verbose_name='手机号')
#     email = models.CharField(max_length=50, verbose_name='邮箱')
#
#     created = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
#     updated = models.DateTimeField(auto_now=True, verbose_name='更新时间')
#     comment = models.CharField(max_length=255, verbose_name='备注', null=True, blank=True)
#
#     @receiver(post_save, sender=User)
#     def create_user_profile(sender, instance, created, **kwargs):
#         if created:
#             Profile.objects.create(user=instance)
#
#     @receiver(post_save, sender=User)
#     def save_user_profile(sender, instance, **kwargs):
#         instance.profile.save()


class Config(models.Model):
    group = models.CharField(max_length=25, verbose_name='配置组')
    item = models.CharField(max_length=255, verbose_name='配置项')
    value = models.CharField(max_length=255, verbose_name='配置值')
    encrypt = models.IntegerField(verbose_name='加密', default=0)
    name = models.CharField(max_length=25, verbose_name='名称')
    created = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    comment = models.CharField(max_length=255, verbose_name='备注', null=True, blank=True)

    